// 
// 数据库管理模块
// 

// import React from 'react';
// import ReactDOM from 'react-dom';
// import AceEditor from 'react-ace';

const LANG = antSword['language']['database'];

class Database {

  constructor(opt) {
    this.hash = (+new Date * Math.random()).toString(16).substr(2, 8);

    // 初始化UI
    const tabbar = antSword['tabbar'];
    tabbar.addTab(
      `tab_database_${this.hash}`,
      `<i class="fa fa-database"></i> ${opt['ip']}`,
      null, null, true, true
    );
    this.cell = tabbar.cells(`tab_database_${this.hash}`);
    this.cell.progressOn();

    // layout
    this.layout_main = this.cell.attachLayout('2U');
    this.layout_right = this.layout_main.cells('b').attachLayout('2E');

    this.list = this.initList(this.layout_main.cells('a'));
    this.query = this.initQuery(this.layout_right.cells('a'));
    this.result = this.initResult(this.layout_right.cells('b'));

    this.opt = opt;
    this.win = new dhtmlXWindows();
    this.win.attachViewportTo(this.cell.cell);

    // 加载数据库驱动
    const _module = require(`./${opt['type']}/index`);
    this.drive = new _module({
      core: new antSword['core'][opt['type']](opt),
      super: this
    });
    this.cell.progressOff();
  }

  // 初始化左侧列表
  initList(layout) {
    layout.setText(`<i class="fa fa-server"></i> ${LANG['list']['title']}`);
    layout.setWidth('250');

    // tree图标
    const imgs = [
      // connect
      '',
      // databass
      '',
      // table
      '',
      // column
      ''
    ];

    // 左侧拦toolbar
    const toolbar = layout.attachToolbar();
    toolbar.loadStruct([
      { id: 'add', text: LANG['list']['add'], icon: 'plus-circle', type: 'button' },
      // { type: 'separator' },
      // { id: 'edit', text: '编辑', icon: 'edit', type: 'button', disabled: true },
      { type: 'separator' },
      { id: 'del', text: LANG['list']['del'], icon: 'trash-o', type: 'button', disabled: true }
    ]);
    toolbar.attachEvent('onClick', (id) => {
      switch(id) {
        case 'add':
          this.drive.addConf();
          break;
        case 'del':
          this.drive.delConf();
          break;
        // case 'edit':
        //   this.drive.editConf();
        //   break;
      }
    });
    return {
      imgs: imgs,
      layout: layout,
      toolbar: toolbar
    };
  }

  // 初始化右侧::SQL执行
  initQuery(layout) {
    layout.setText(`<i class="fa fa-code"></i> ${LANG['query']['title']}`);
    layout.setHeight('200');

    let editor;
    // SQL语句toolbar
    const toolbar = layout.attachToolbar();
    toolbar.loadStruct([
      { id: 'exec', text: LANG['query']['exec'], icon: 'play', type: 'button', disabled: true },
      // { type: 'separator' },
      // { id: 'import', text: '导入', icon: 'download', type: 'button' },
      { type: 'separator' },
      { id: 'clear', text: LANG['query']['clear'], icon: 'remove', type: 'button' }
    ]);

    toolbar.attachEvent('onClick', (id) => {
      switch(id) {
        case 'clear':
          editor.session.setValue('');
          break;
        case 'exec':
          this.drive.execSQL(editor.session.getValue());
          break;
      }
    });

    // SQL语句编辑器
    editor = ace.edit(layout.cell.lastChild);
    editor.$blockScrolling = Infinity;
    editor.setTheme('ace/theme/tomorrow');
    editor.session.setMode('ace/mode/sql');
    editor.session.setUseWrapMode(true);
    editor.session.setWrapLimitRange(null,null);

    editor.setOptions({
      fontSize: '14px',
      enableBasicAutocompletion: true,
      enableSnippets: true,
      enableLiveAutocompletion: true
    });

    // 快捷键
    editor.commands.addCommand({
      name: 'exec',
      bindKey: {
        win: 'Ctrl-E',
        mac: 'Command-E'
      },
      exec: () => {
        toolbar.callEvent('onClick', ['exec']);
      }
    });

    editor.session.setValue('SELECT "Hello antSword :)" AS welcome;');

    return {
      editor: editor,
      layout: layout,
      toolbar: toolbar
    };
  }

  // 初始化右侧::执行结果
  initResult(layout) {
    layout.setText(`<i class="fa fa-inbox"></i> ${LANG['result']['title']}`);
    // layout.hideHeader();

    // const toolbar = layout.attachToolbar();
    // toolbar.loadStruct([
    //   { id: 'dump', text: '导出', icon: 'upload', type: 'button', disabled: true },
    //   { type: 'separator' }
    // ]);
    return {
      layout: layout,
      // toolbar: toolbar
    };
  }

  // 创建窗口
  createWin(opts) {
    const hash = (+new Date * Math.random()).toString(16).substr(2, 8);
    // 默认配置
    const opt = $.extend({
      title: 'Window:' + hash,
      width: 550,
      height: 450
    }, opts);

    // 创建窗口
    const _win = this.win.createWindow(hash, 0, 0, opt['width'], opt['height']);
    _win.setText(opt['title']);
    _win.centerOnScreen();
    _win.button('minmax').show();
    _win.button('minmax').enable();

    // 返回窗口对象
    return _win;
  }

}

export default Database;